package com.fishery.customization.dao;

import com.fishery.customization.dao.reposity.BaseDao;
import com.fishery.customization.entity.DraggableModuleChecked;
import com.fishery.customization.entity.vo.UpdateDraggableModuleCheckedVo;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Repository
public class DraggableModuleCheckedDao extends BaseDao<DraggableModuleChecked> {

	public List<String> findDraggableModuleCheckedByUserId(String userId) {
		Map<String, Object> fields = new HashMap<>(2);
		fields.put("_id", 0);
		fields.put("module_checked", 1);
		return mongoOperations.findOne(getBasicQuery("user_id", userId, fields),
				DraggableModuleChecked.class).getModuleChecked();
	}


	public long upsertDraggableModuleCheckedByUserId(UpdateDraggableModuleCheckedVo draggableModuleCheckedVo) {
		Update update = new Update();
		update.set("module_checked", draggableModuleCheckedVo.getModuleChecked());
		return mongoOperations.upsert(Query.query(Criteria.where("user_id")
				.is(draggableModuleCheckedVo.getUserId())), update, getCollectionName()).wasAcknowledged() ? 1 : 0;
	}
}
